<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>FabGL: VGA/DirectVGA/DirectVGA.ino</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">FabGL
   </div>
   <div id="projectbrief">ESP32 Display Controller and Graphics Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_v_g_a_2_direct_v_g_a_2_direct_v_g_a_8ino-example.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">VGA/DirectVGA/DirectVGA.ino</div>  </div>
</div><!--header-->
<div class="contents">
<p>Sample usage of VGADirectController base class for direct VGA control</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - &lt;http://www.fabgl.com&gt;</span></div><div class="line"><span class="comment">  Copyright (c) 2019-2021 Fabrizio Di Vittorio.</span></div><div class="line"><span class="comment">  All rights reserved.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">* Please contact fdivitto2013@gmail.com if you need a commercial license.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">* This library and related software is available under GPL v3.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is free software: you can redistribute it and/or modify</span></div><div class="line"><span class="comment">  it under the terms of the GNU General Public License as published by</span></div><div class="line"><span class="comment">  the Free Software Foundation, either version 3 of the License, or</span></div><div class="line"><span class="comment">  (at your option) any later version.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is distributed in the hope that it will be useful,</span></div><div class="line"><span class="comment">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div><div class="line"><span class="comment">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div><div class="line"><span class="comment">  GNU General Public License for more details.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  You should have received a copy of the GNU General Public License</span></div><div class="line"><span class="comment">  along with FabGL.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="fabgl_8h.html">fabgl.h</a>&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><a name="_a0"></a><a class="code" href="classfabgl_1_1_v_g_a_direct_controller.html">fabgl::VGADirectController</a> DisplayController;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">volatile</span> <span class="keywordtype">double</span>      objX    = 300;</div><div class="line"><span class="keyword">volatile</span> <span class="keywordtype">double</span>      objY    = 200;</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span>        objDir  = 0.3;</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span>        objVel  =  10;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> objSize =  25;</div><div class="line"><span class="keyword">static</span> TaskHandle_t  mainTaskHandle;</div><div class="line"><span class="keyword">static</span> uint32_t      bgParkMillerState = 1;</div><div class="line"><span class="keyword">static</span> constexpr <span class="keywordtype">int</span> borderSize = 20;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// just to avoid floating point calculations inside MyDirectDrawVGAController::drawScanline()</span></div><div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> objIntX;</div><div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> objIntY;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">inline</span> <span class="keywordtype">int</span> fastRandom()</div><div class="line">{</div><div class="line">  bgParkMillerState = (uint64_t)bgParkMillerState * 48271 % 0x7fffffff;</div><div class="line">  <span class="keywordflow">return</span> bgParkMillerState % 4;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> IRAM_ATTR drawScanline(<span class="keywordtype">void</span> * arg, uint8_t * dest, <span class="keywordtype">int</span> scanLine)</div><div class="line">{</div><div class="line">  <span class="keyword">auto</span> fgcolor = DisplayController.createRawPixel(RGB222(3, 0, 0)); <span class="comment">// red</span></div><div class="line">  <span class="keyword">auto</span> bgcolor = DisplayController.createRawPixel(RGB222(0, 0, 2)); <span class="comment">// blue</span></div><div class="line"></div><div class="line">  <span class="comment">// fill upper and lower border with random background color</span></div><div class="line">  <span class="keywordflow">if</span> (scanLine &lt; borderSize || scanLine &gt; DisplayController.<a name="a1"></a><a class="code" href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">getScreenHeight</a>() - borderSize)</div><div class="line">    bgcolor = DisplayController.createRawPixel(RGB222(fastRandom(), fastRandom(), fastRandom()));</div><div class="line"></div><div class="line">  <span class="keyword">auto</span> <a name="a2"></a><a class="code" href="displaycontroller_8h_a09a2a45f731b02946ff6d3cd15c1a476.html#a09a2a45f731b02946ff6d3cd15c1a476">width</a> = DisplayController.<a name="a3"></a><a class="code" href="classfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c.html#a749df909b39fd096bf6e2813f2f5324c">getScreenWidth</a>();</div><div class="line"></div><div class="line">  <span class="comment">// fill line with background color</span></div><div class="line">  memset(dest, bgcolor, <a class="code" href="displaycontroller_8h_a09a2a45f731b02946ff6d3cd15c1a476.html#a09a2a45f731b02946ff6d3cd15c1a476">width</a>);</div><div class="line"></div><div class="line">  <span class="comment">// fill object with foreground color</span></div><div class="line">  <span class="keywordflow">if</span> (scanLine &gt;= objIntY - objSize / 2 &amp;&amp; scanLine &lt;= objIntY + objSize / 2) {</div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> col = objIntX - objSize / 2; col &lt; objIntX + objSize / 2; ++col) {</div><div class="line">      <span class="keywordflow">if</span> (col &gt;= 0 &amp;&amp; col &lt; <a class="code" href="displaycontroller_8h_a09a2a45f731b02946ff6d3cd15c1a476.html#a09a2a45f731b02946ff6d3cd15c1a476">width</a>) {</div><div class="line">        VGA_PIXELINROW(dest, col) = fgcolor;</div><div class="line">      }</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (scanLine == DisplayController.<a class="code" href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">getScreenHeight</a>() - 1) {</div><div class="line">    <span class="comment">// signal end of screen</span></div><div class="line">    vTaskNotifyGiveFromISR(mainTaskHandle, NULL);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> setup()</div><div class="line">{</div><div class="line">  <span class="comment">//Serial.begin(115200); delay(500); Serial.write(&quot;\n\n\n&quot;); // DEBUG ONLY</span></div><div class="line"></div><div class="line">  mainTaskHandle = xTaskGetCurrentTaskHandle();</div><div class="line"></div><div class="line">  DisplayController.begin();</div><div class="line">  DisplayController.<a name="a4"></a><a class="code" href="classfabgl_1_1_v_g_a_direct_controller_a18497a8f427eca28ee4bc4fed3a76508.html#a18497a8f427eca28ee4bc4fed3a76508">setDrawScanlineCallback</a>(drawScanline);</div><div class="line">  DisplayController.<a name="a5"></a>setResolution(<a name="a6"></a><a class="code" href="fabglconf_8h_a2d4571e9072b78877c36bf32bc9423f8.html#a2d4571e9072b78877c36bf32bc9423f8">VGA_640x480_60Hz</a>);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> loop()</div><div class="line">{</div><div class="line">  <span class="comment">// test collision with borders and bounce changing direction</span></div><div class="line">  <span class="keywordflow">if</span> (objX &lt; objSize / 2 || objX &gt; DisplayController.<a class="code" href="classfabgl_1_1_base_display_controller_a749df909b39fd096bf6e2813f2f5324c.html#a749df909b39fd096bf6e2813f2f5324c">getScreenWidth</a>() - objSize / 2)</div><div class="line">    objDir = PI - objDir;</div><div class="line">  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (objY &lt; borderSize + objSize / 2 || objY &gt; DisplayController.<a class="code" href="classfabgl_1_1_base_display_controller_a0872427e4a351bb3590e8d00096e9cba.html#a0872427e4a351bb3590e8d00096e9cba">getScreenHeight</a>() - borderSize - objSize / 2)</div><div class="line">    objDir = 2 * PI - objDir;</div><div class="line"></div><div class="line">  <span class="comment">// calculate new coordinates</span></div><div class="line">  objX += objVel * cos(objDir);</div><div class="line">  objY += objVel * sin(objDir);</div><div class="line"></div><div class="line">  <span class="comment">// convert object coordinate to integer</span></div><div class="line">  objIntX = objX;</div><div class="line">  objIntY = objY;</div><div class="line"></div><div class="line">  <span class="comment">// wait for vertical sync</span></div><div class="line">  ulTaskNotifyTake(pdTRUE, portMAX_DELAY);</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Mon Aug 2 2021 07:08:02 for FabGL by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
